package com.design.pattern.flyweight.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Vector;

/**
 * @version 1.0
 * @author: CRM_SYSTEM
 * @date: 2022/10/18 22:33
 * @Description:
 */
public class ConnectionPool {

    public static void main(String[] args) {
        String redistributeSql1 = "(( opportunity_status= " + "12313"
                + ") or ( opportunity_status =" + "3423" +
                " and redistribute_status = " + "23423" + "))";

        System.out.println(redistributeSql1);
    }

    private Vector<Connection>  pool;


    private String url="jdbc:mysql://localhost:3306/ai_ftx";
    private String userName="root";
    private String passWord="root";
    private String driverClassName="com.mysql.jdbc.Driver";


    private int poolSize=100;

    public ConnectionPool(){
        new Vector<ConnectionPool>(poolSize);

        for (int i = 0; i < poolSize; i++) {
            try {
                Class.forName(driverClassName);
                Connection connection = DriverManager.getConnection(url, userName, passWord);
                pool.add(connection);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
    }

    public synchronized Connection getConnection(){
        if (pool.size()>0){
            Connection connection = pool.get(0);
            pool.remove(connection);
            return connection;
        }
        return null;
    }
    public synchronized void release(Connection connection){
        pool.add(connection);
    }
}
